package com.yakami.ocean.view.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yakami.ocean.R;
import com.yakami.ocean.ServerAPI;
import com.yakami.ocean.event.Event;
import com.yakami.ocean.event.RxBus;
import com.yakami.ocean.view.fragment.base.BaseFragment;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Yakami on 2016/5/21, enjoying it!
 */
public class UserInfoFragment extends BaseFragment {

    @Bind(R.id.user_name) TextView mUserName;
    @Bind(R.id.user_id) TextView mUserId;
    @Bind(R.id.email) TextView mEmail;
    @Bind(R.id.date) TextView mDate;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceBundle) {
        View view = inflater.inflate(R.layout.fragment_user_info, container, false);
        ButterKnife.bind(this, view);

        ServerAPI.getOceanAPI()
                .getUserDetailedInfo("个人资料")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(info -> {
                    mUserName.setText(info.getName());
                    mUserId.setText(info.getUser_id() + "");
                    mEmail.setText(info.getEmail());
                    mDate.setText(info.getSignUpDate());
                    RxBus.getInstance().send(Event.EventType.LOADED_SUCCESSFUL, null);
                }, throwable -> {
                    throwable.printStackTrace();
                    RxBus.getInstance().send(Event.EventType.LOAD_ERROR, null);
                });
        return view;
    }

    public static  UserInfoFragment newInstance() {
        return new UserInfoFragment();
    }
}
